package com.behome.netty.netty_D.bean;

import com.behome.netty.common.ByteConventerKit;
import com.behome.netty.netty_D.enums.EbmClass;
import com.behome.netty.netty_D.enums.EbmLevel;
import com.behome.netty.netty_D.enums.TerminalDAnnotation;
import com.behome.netty.netty_D.enums.TerminalDType;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @description: 终端D启动广播
 * @author bh_ljy
 * @date: 2024/5/27 15:15
 */
@TerminalDAnnotation(
        type = TerminalDType.START_BROADCAST
)
public class TerminalDStartBroadcast extends BusinessCmdBase {
    private String ebmid;
    private EbmClass ebmClass;
    private EbmLevel ebmLevel;
    private String ebmType = "11111";
    private byte volume;
    private Date startTime;
    private Date endTime;
    private byte auxiliaryNumber;
    private Auxiliary[] auxiliaries;
    private static final int MIN_BUSI_LENGTH = 35;

    public EbmClass getEbmClass() {
        return this.ebmClass;
    }

    public void setEbmClass(EbmClass ebmClass) {
        this.ebmClass = ebmClass;
    }

    public EbmLevel getEbmLevel() {
        return this.ebmLevel;
    }

    public void setEbmLevel(EbmLevel ebmLevel) {
        this.ebmLevel = ebmLevel;
    }

    public String getEbmType() {
        return this.ebmType;
    }

    public void setEbmType(String ebmType) {
        this.ebmType = ebmType;
    }

    public byte getVolume() {
        return this.volume;
    }

    public void setVolume(byte volume) {
        this.volume = volume;
    }

    public Date getStartTime() {
        return this.startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return this.endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public byte getAuxiliaryNumber() {
        return this.auxiliaryNumber;
    }

    public void setAuxiliaryNumber(byte auxiliaryNumber) {
        this.auxiliaryNumber = auxiliaryNumber;
    }

    public Auxiliary[] getAuxiliaries() {
        return this.auxiliaries;
    }

    public void setAuxiliaries(Auxiliary[] auxiliaries) {
        this.auxiliaries = auxiliaries;
    }

    public String getEbmid() {
        return this.ebmid;
    }

    public void setEbmid(String ebmid) {
        this.ebmid = ebmid;
    }

    public byte[] serialize() {
        if (!this.validateBeforeSerialize()) {
            return null;
        } else {
            int length = MIN_BUSI_LENGTH;
            if (this.auxiliaries != null) {
                for (Auxiliary auxiliary : this.auxiliaries) {
                    length += auxiliary.serialize().length;
                }
            }
            ByteBuffer byteBuffer = ByteBuffer.allocate(length);
            byteBuffer.put(ByteConventerKit.strToBcd(this.ebmid))
                    .put(this.ebmClass.getValue())
                    .put(this.ebmLevel.getValue())
                    .put(this.ebmType.getBytes(StandardCharsets.US_ASCII))
                    .put(this.volume)
                    .put(ByteConventerKit.datetimeToBytes(this.startTime))
                    .put(ByteConventerKit.datetimeToBytes(this.endTime))
                    .put(this.auxiliaryNumber);
            if (this.auxiliaries != null) {
                for (Auxiliary auxiliary : this.auxiliaries) {
                    byteBuffer.put(auxiliary.serialize());
                }
            }
            return byteBuffer.array();
        }
    }

    public boolean validateBeforeSerialize() {
        return true;
    }

    public BusinessCmdBase deserialize(byte[] businessData) {
        if (businessData.length < MIN_BUSI_LENGTH) {
            throw new RuntimeException("开播数据总长度(" + businessData.length + ")小于最小数据长度:" + MIN_BUSI_LENGTH);
        } else {
            ByteBuffer byteBuffer = ByteBuffer.wrap(businessData);
            byte[] ebmIdData = new byte[18];
            byteBuffer.get(ebmIdData);
            this.ebmid = ByteConventerKit.deserializeEbmId(ebmIdData);
            this.ebmClass = EbmClass.getEnumByValue(byteBuffer.get());
            this.ebmLevel = EbmLevel.getEnumByValue(byteBuffer.get());
            byte[] ebmTypeData = new byte[5];
            byteBuffer.get(ebmTypeData);
            this.ebmType = new String(ebmTypeData, StandardCharsets.US_ASCII);
            this.volume = byteBuffer.get();
            byte[] startTimeData = new byte[4];
            byteBuffer.get(startTimeData);
            this.startTime = ByteConventerKit.bytesToDatetime(startTimeData);
            byte[] endTimeData = new byte[4];
            byteBuffer.get(endTimeData);
            this.endTime = ByteConventerKit.bytesToDatetime(endTimeData);
            this.auxiliaryNumber = byteBuffer.get();
            if (this.auxiliaryNumber > 0) {
                Auxiliary[] auxiliaries = new Auxiliary[this.auxiliaryNumber];
                for (int i = 0; i < this.auxiliaryNumber; ++i) {
                    Auxiliary auxiliary = new Auxiliary();
                    auxiliary.setDataType(byteBuffer.get());
                    short length = byteBuffer.getShort();
                    if (length > 0) {
                        byte[] auxilaryData = new byte[length];
                        byteBuffer.get(auxilaryData);
                        auxiliary.setData(auxilaryData);
                    }
                    auxiliaries[i] = auxiliary;
                }
                this.auxiliaries = auxiliaries;
            }
            return this;
        }
    }


}
